//
//  RBShopApplyCatPopVC.m
//  zhongzhenglu
//
//  Created by XL on 2022/8/31.
//

#import "RBShopApplyCatPopVC.h"
#import "RBDefine.h"
#import "UIView+RBAdd.h"
#import "UIScrollView+RBAdd.h"
#import "RBThemeConfig.h"
#import <Masonry.h>
#import "RBNavigationBar.h"
#import "RBTextField.h"
#import "UIButton+RBAdd.h"
#import <UIColor+YYAdd.h>
#import "UIViewController+RBAdd.h"
#import "RBPager.h"
#import "RanTool.h"
#import <UIView+YYAdd.h>
#import "RBTagContainerView.h"

@interface RBShopApplyCatPopVC ()

@property(nonatomic, strong)NSMutableArray *tagViews;

@property(nonatomic, strong) NSArray *tags;
@property(nonatomic, assign)NSInteger tagSelIndex;

@end

@implementation RBShopApplyCatPopVC

#pragma mark - Lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setup];
}

#pragma mark - Private

- (void)setup
{
    self.dismissOnTapBlank = YES;
    self.view.backgroundColor = UIColor.clearColor;
    
    self.contentView = UIView.new;
    [self.view addSubview:self.contentView];
    self.contentView.backgroundColor = UIColor.whiteColor;
    [self.contentView rbSetCorner:UIRectCornerTopLeft | UIRectCornerTopRight  radius:Zoom(8)];
    
    [self.contentView view_addView:({
        UIView *view = UIView.new;
        [view view_addView:[RanTool labelWithFontSize:Zoom(16) weight:0 color:[RBThemeConfig textBlackColor] align:0 text:@"主营类目"] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:CGFLOAT_MIN right:CGFLOAT_MIN centerX:YES];
        
        UIImageView *closeImgView = [RanTool imgViewWithMode:0 radius:0 imgName:@"goodsDetailPramsClose"];
        [closeImgView extendResponseArea];
        [closeImgView rbAddTapGesWithTarget:self action:@selector(dismiss)];
        [view addSubview:closeImgView];
        [closeImgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.mas_equalTo(Zoom(19));
            make.right.mas_equalTo(-Zoom(16));
            make.centerY.mas_equalTo(0);
        }];
        
        view;
    }) withHeight:Zoom(68) width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerX:0];
    
    NSArray *tags = self.tags;
    for (NSInteger i = 0; i < tags.count; i++)
    {
        [self.tagViews addObject:({
            UILabel *view = [RanTool labelWithFontSize:Zoom(12) weight:0 color:[RBThemeConfig textBlackColor] align:1 text:tags[i] bgColor:UIColor.whiteColor radius:Zoom(15)];
            view.tag = i;
            [view rbAddTapGesWithTarget:self action:@selector(tagViewTapped:)];
            view.width = Zoom(94);
            view.height = Zoom(30);
            view;
        })];
    }
    
    RBTagContainerView *tagContainerView = RBTagContainerView.new;
    tagContainerView.constraintWidth = kRBMainScreenWidth - Zoom(40);
    tagContainerView.hMargin = Zoom(20);
    tagContainerView.vMargin = Zoom(18);
    [tagContainerView addTagViews:self.tagViews];
    
    [self.contentView view_addView:tagContainerView withHeight:tagContainerView.calulatedHeight width:tagContainerView.constraintWidth top:0 bottom:0 left:CGFLOAT_MIN right:CGFLOAT_MIN centerX:YES];
    
    [self.contentView view_addView:({
        UILabel *view = [RanTool labelWithFontSize:Zoom(15) weight:0 color:UIColor.whiteColor align:1 text:@"确定" bgColor:[RBThemeConfig buttonBlueColor] radius:Zoom(20)];
        [view rbAddTapGesWithTarget:self action:@selector(confirmBtnTapped)];
        view;
    }) withHeight:Zoom(40) width:Zoom(350) top:Zoom(40) bottom:MAX(kRBSafeBottomHeight, Zoom(20)) left:CGFLOAT_MIN right:CGFLOAT_MIN centerX:YES];
    
    self.contentView.left = 0;
    self.contentView.width = kRBMainScreenWidth;
    self.contentView.height = self.contentView.esSize.height;
    self.contentView.top = kRBMainScreenHeight;
    
    self.tagSelIndex = self.tagSelIndex;
}

- (void)show
{
    [UIView animateWithDuration:0.15 animations:^{
        self.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
        self.contentView.bottom = kRBMainScreenHeight;
    }];
}

- (void)dismiss
{
    [UIView animateWithDuration:0.15 animations:^{
        self.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
        self.contentView.top = kRBMainScreenHeight;
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:nil];
    }];
}

#pragma mark - Public

+ (RBShopApplyCatPopVC *)vcWithTags:(NSArray *)tags index:(NSInteger)index
{
    RBShopApplyCatPopVC *vc = RBShopApplyCatPopVC.new;
    vc.tags = tags;
    vc.tagSelIndex = index;
    return vc;
}

#pragma mark - Network

#pragma mark - Event

- (void)tagViewTapped: (UITapGestureRecognizer *)ges
{
    self.tagSelIndex = ges.view.tag;
}

- (void)confirmBtnTapped
{
    if(self.tagSelIndex < 0)
    {
        [self.contentView showTip:@"请选择主营类目" completion:nil];
        return;
    }
    if(self.block)
    {
        self.block(self.tagSelIndex);
    }
    [self dismiss];
}

#pragma mark - Setter

- (void)setTagSelIndex:(NSInteger)tagSelIndex
{
    _tagSelIndex = tagSelIndex;
    for (UIView *view in self.tagViews)
    {
        view.backgroundColor = tagSelIndex == view.tag? [UIColor colorWithHexString:@"#C5D5FF"]: [UIColor colorWithHexString:@"#F2F2F2"];
    }
}

#pragma mark - Getter

- (NSMutableArray *)tagViews
{
    if (!_tagViews)
    {
        self.tagViews = NSMutableArray.array;
    }
    return _tagViews;
}

@end
